//
//  AVPlayerViewController.m
//  EDPS
//
//  Created by 张维扬 on 15/12/3.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "AVPlayerViewController.h"
#import "PlayerTableViewCell.h"
#import "ZWY_NetHandle.h"
#import "PlayerModel.h"
#import "AudioPlayerViewController.h"
@interface AVPlayerViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation AVPlayerViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonPressed:)];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 左面按钮
    // 初始化一个返回按钮
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    // 为返回按钮设置图片样式
    [button setImage:[UIImage imageNamed:@"iconfont-back"] forState:UIControlStateNormal];
    // 设置返回按钮触发的事件
    [button addTarget:self action:@selector(backBarButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    // 初始化一个BarButtonItem，并将其设置为返回的按钮的样式
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    // 将BarButtonItem添加到LeftBarButtonItem上
    self.navigationItem.leftBarButtonItem = backButton;
    // 请求数据
    [self getData];
    // 创建
    [self createTableView];
    
    
    
}
- (void)backBarButtonPressed: (UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 获取数据
- (void)getData
{
    NSString *url = @"http://baobab.wandoujia.com/api/v1/videos?start=0&num=10&categoryName=旅行&strategy=date";
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [ZWY_NetHandle getURL:urlStr Cookie:nil parameter:nil result:^(id result) {
        NSLog(@"%@", result);
        self.dataArr = [PlayerModel transformWithArray:result[@"videoList"]];
        
        [self.tableView reloadData];
    }];
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    [self.tableView registerClass:[PlayerTableViewCell class] forCellReuseIdentifier:@"player"];
    [self.view addSubview:self.tableView];
    
}
#pragma mark - 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"player"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenWidth * 0.6;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioPlayerViewController *audio = [[AudioPlayerViewController alloc] init];
    audio.model = self.dataArr[indexPath.row];
    [self presentViewController:audio animated:YES completion:nil];
}



@end
